package com.qzq.weibo.http;

import java.io.File;
import java.io.FileNotFoundException;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.qzq.weibo.utils.Constants;
import com.qzq.weibo.utils.Utils;
import com.qzq.weibo.utils.WeiCozzz;

public class HttpRequest {

	private static HttpRequest mInstance;

	private FinalHttp finalHttp;

	private AjaxParams params;

	private SharedPreferences pref;
	private String uid;
	private String access_token;

	/**
	 * 网络请求失败
	 */
	public static final int REQUEST_FAILER = 0;

	/**
	 * 网络请求成功
	 */
	public static final int REQUEST_SUCCESS = 1;

	/**
	 * 网络连接异常
	 */
	public static final int NO_NETWORK_CONNECTION = 0;

	/**
	 * 请求参数不正确
	 */
	public static final int ERROR_PARAM = 400;

	/**
	 * 错误的请求方式
	 */
	public static final int ERROR_REQUEST_METHOD = 405;

	/**
	 * 未知错误
	 */
	public static final int UNKNOW_ERROR = 500;

	/**
	 * 服务器错误
	 */

	private static Utils utils;

	public static final int SERVICE_ERROR = 502;

	private static final String TAG = "HttpRequest";

	private HttpRequest(Context context) {
		this.finalHttp = new FinalHttp();

		this.pref = context.getSharedPreferences(Constants.PREFERENCES_NAME,
				Context.MODE_APPEND);

		access_token = pref.getString(Constants.KEY_ACCESS_TOKEN, null);
		uid = pref.getString(Constants.KEY_UID, null);
	}

	public static HttpRequest getInstance(Context context) {
		if (mInstance == null) {
			mInstance = new HttpRequest(context);
		}

		return mInstance;
	}

	/**
	 * 错误请求的Toast提示
	 * 
	 * @param errNo
	 *            错误码
	 * @param context
	 *            错误提示
	 */
	public static void badRequest(int errNo, Context context) {

		utils = Utils.getInstance(context);

		switch (errNo) {

		case NO_NETWORK_CONNECTION:

			utils.setToastMsg("网络异常");
			break;

		case ERROR_PARAM:

			utils.setToastMsg("错误的请求参数");

			break;

		case ERROR_REQUEST_METHOD:
			utils.setToastMsg("错误的请求方式");
			break;

		case SERVICE_ERROR:
			utils.setToastMsg("服务器错误");
			break;

		case UNKNOW_ERROR:
			utils.setToastMsg("未知错误");
			break;

		default:
			utils.setToastMsg("未知错误");
			break;
		}

	}

	/**
	 * 请求用户数据
	 * 
	 * @param mHandler
	 */
	public void requestGetUserInfo(final Handler mHandler) {
		String url = "https://api.weibo.com/2/users/show.json";
		params = new AjaxParams();

		params.put("uid", uid);
		params.put("access_token", access_token);

		Log.d(TAG, "requestGetUserInfo uid :" + uid);
		Log.d(TAG, "requestGetUserInfo access_token :" + access_token);

		finalHttp.get(url, params, new AjaxCallBack<Object>() {
			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Log.d(TAG, "requestGetUserInfo fail :" + errorNo);
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_FAILER;
				msg.obj = errorNo;
				msg.sendToTarget();
			}

			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				Log.d(TAG, "requestGetUserInfo success :" + t.toString());
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_SUCCESS;
				JsonHelper helper = new JsonHelper();
				msg.obj = helper.getUserInfo(t.toString());
				msg.sendToTarget();
			}
		});
	}

	/**
	 * 发布文字微博
	 * 
	 * @param mHandler
	 * @param status
	 *            微博文字内容
	 */
	public void requestPublishTextStatus(final Handler mHandler, String status) {

		String url = "https://upload.api.weibo.com/2/statuses/update.json";
		params = new AjaxParams();
		params.put("access_token", access_token);
		params.put("status", status);

		finalHttp.post(url, params, new AjaxCallBack<Object>() {
			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Log.d(TAG, "requestPublishStatus fail :" + errorNo);
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_FAILER;
				msg.obj = errorNo;
				msg.sendToTarget();
			}

			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				Log.d(TAG, "requestPublishStatus success :" + t.toString());
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_SUCCESS;
				msg.obj = t.toString();
				msg.sendToTarget();
			}
		});
	}

	/**
	 * 发布带图片的微博
	 * 
	 * @param mHandler
	 * @param status
	 *            微博内容
	 * @param pic
	 *            微博配图
	 */
	public void requestPublishImageStatus(final Handler mHandler,
			String status, File pic) {

		String url = "https://upload.api.weibo.com/2/statuses/upload.json";
		params = new AjaxParams();
		params.put("access_token", access_token);
		params.put("status", status);
		try {
			params.put("pic", pic);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		finalHttp.post(url, params, new AjaxCallBack<Object>() {
			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Log.d(TAG, "requestPublishImageStatus fail :" + errorNo);
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_FAILER;
				msg.obj = errorNo;
				msg.sendToTarget();
			}

			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				Log.d(TAG, "requestPublishImageStatus success :" + t.toString());
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_SUCCESS;
				msg.obj = t.toString();
				msg.sendToTarget();
			}
		});

	}

	/**
	 * 获取主页的微博列表
	 * 
	 * @param mHandler
	 * @param page
	 *            页码
	 * @param count
	 *            每页数量
	 */
	public void requestGetHomeStatus(final Handler mHandler, int page, int count) {

		String url = "https://api.weibo.com/2/statuses/home_timeline.json";

		params = new AjaxParams();
		params.put("access_token", access_token);
		params.put("page", String.valueOf(page));
		params.put("count", String.valueOf(count));

		finalHttp.get(url, params, new AjaxCallBack<Object>() {
			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Log.d(TAG, "requestGetPublicStatus fail :" + errorNo);
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_FAILER;
				msg.obj = errorNo;
				msg.sendToTarget();
			}

			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				Log.d(TAG, "requestGetPublicStatus success :" + t.toString());
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_SUCCESS;
				JsonHelper helper = new JsonHelper();
				msg.obj = helper.getStatus(t.toString());
				msg.sendToTarget();
			}
		});

	}

	/**
	 * 获取好友圈微博列表
	 * 
	 * @param mHandler
	 * @param page
	 *            页码
	 * @param count
	 *            每页数量
	 */
	public void requestGetFriendsStatus(final Handler mHandler, int page,
			int count) {
		String url = "https://api.weibo.com/2/statuses/bilateral_timeline.json";
		params = new AjaxParams();
		params.put("access_token", access_token);
		params.put("page", String.valueOf(page));
		params.put("count", String.valueOf(count));

		finalHttp.get(url, params, new AjaxCallBack<Object>() {
			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Log.d(TAG, "requestGetFriendsStatus fail :" + errorNo);
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_FAILER;
				msg.obj = errorNo;
				msg.sendToTarget();
			}

			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				Log.d(TAG, "requestGetFriendsStatus success :" + t.toString());
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_SUCCESS;
				JsonHelper helper = new JsonHelper();
				msg.obj = helper.getStatus(t.toString());
				msg.sendToTarget();
			}
		});

	}

	/**
	 * 获取特定微博的评论列表
	 * 
	 * @param mHandler
	 * @param idstr
	 *            微博id
	 * @param page
	 *            页码
	 * @param count
	 *            每页数量
	 */
	public void requestGetComments(final Handler mHandler, String idstr,
			int page, int count) {
		String url = "https://api.weibo.com/2/comments/show.json";
		params = new AjaxParams();
		params.put("access_token", access_token);
		params.put("id", idstr);
		params.put("page", String.valueOf(page));
		params.put("count", String.valueOf(count));

		finalHttp.get(url, params, new AjaxCallBack<Object>() {
			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Log.d(TAG, "requestGetComments fail :" + errorNo);
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_FAILER;
				msg.obj = errorNo;
				msg.sendToTarget();

			}

			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				Log.d(TAG, "requestGetComments success :" + t.toString());
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_SUCCESS;
				JsonHelper helper = new JsonHelper();
				msg.obj = helper.getComments(t.toString());
				msg.sendToTarget();
			}
		});
	}

	/**
	 * 评论微博
	 * 
	 * @param mHandler
	 * @param comment
	 *            评论内容
	 * @param status_id
	 *            微博id
	 */
	public void requestReplyStatus(final Handler mHandler, String comment,
			String status_id) {
		String url = "https://api.weibo.com/2/comments/create.json";
		params = new AjaxParams();
		params.put("access_token", access_token);
		params.put("comment", comment);
		params.put("id", status_id);

		System.out.println("id:" + status_id + "comment:" + comment);

		finalHttp.post(url, params, new AjaxCallBack<Object>() {
			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Log.d(TAG, "requestReplyStatus fail :" + errorNo);
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_FAILER;
				msg.obj = errorNo;
				msg.sendToTarget();
			}

			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				Log.d(TAG, "requestReplyStatus success :" + t.toString());
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_SUCCESS;
				msg.obj = t.toString();
				msg.sendToTarget();
			}
		});

	}

	/**
	 * 回复评论
	 * 
	 * @param mHandler
	 * @param comment
	 *            ：评论内容
	 * @param sid
	 *            ：评论微博的id
	 * @param cid
	 *            :回复评论的id
	 */
	public void requestReplyComment(final Handler mHandler, String comment,
			String sid, String cid) {
		String url = "https://api.weibo.com/2/comments/reply.json";
		params = new AjaxParams();
		params.put("access_token", access_token);
		params.put("comment", comment);
		params.put("id", sid);
		params.put("cid", cid);

		finalHttp.post(url, params, new AjaxCallBack<Object>() {
			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Log.d(TAG, "requestReplyComment fail :" + errorNo);
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_FAILER;
				msg.obj = errorNo;
				msg.sendToTarget();
			}

			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				Log.d(TAG, "requestReplyComment success");
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_SUCCESS;
				msg.obj = t.toString();
				msg.sendToTarget();
			}
		});

	}

	/**
	 * 转发微博
	 * 
	 * @param mHandler
	 * @param status
	 *            :转发内容
	 * @param status_id
	 *            :微博id
	 */
	public void requestRetweetStatus(final Handler mHandler, String status,
			String status_id) {
		String url = "https://api.weibo.com/2/statuses/repost.json";
		params = new AjaxParams();
		params.put("access_token", access_token);
		params.put("id", status_id);
		if (!status.equals("")) {
			params.put("status", status);
		}

		finalHttp.post(url, params, new AjaxCallBack<Object>() {
			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Log.d(TAG, "requestRetweetStatus fail :" + errorNo);
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_FAILER;
				msg.obj = errorNo;
				msg.sendToTarget();
			}

			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				Log.d(TAG, "requestRetweetStatus success :" + t.toString());
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_SUCCESS;
				msg.obj = t.toString();
				msg.sendToTarget();
			}
		});

	}

	/**
	 * 获取未读消息数量
	 * 
	 * @param mhandler
	 */
	public void requestGetUnreadCount(final Handler mhandler) {

		String url = "https://rm.api.weibo.com/2/remind/unread_count.json";
		params = new AjaxParams();
		params.put("access_token", access_token);
		String uid = WeiCozzz.getInstance().getUser().getIdstr();
		params.put("uid", uid);

		finalHttp.get(url, params, new AjaxCallBack<Object>() {
			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Log.d(TAG, "requestGetUnreadCount fail :" + errorNo);
				Message msg = mhandler.obtainMessage();
				msg.what = REQUEST_FAILER;
				msg.obj = errorNo;
				msg.sendToTarget();
			}

			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				Log.d(TAG, "requestGetUnreadCount success :" + t.toString());
				Message msg = mhandler.obtainMessage();
				msg.what = REQUEST_SUCCESS;
				JsonHelper helper = new JsonHelper();
				msg.obj = helper.getUreadCount(t.toString());
				msg.sendToTarget();
			}
		});
	}

	/**
	 * 获取登陆用户的微博列表
	 * 
	 * @param mHandler
	 * @param page
	 *            页码
	 * @param count
	 *            每页数量
	 */
	public void requestGetUserStatues(final Handler mHandler, int page,
			int count) {

		String url = "https://api.weibo.com/2/statuses/user_timeline.json";
		params = new AjaxParams();
		params.put("access_token", access_token);
		params.put("page", String.valueOf(page));
		params.put("count", String.valueOf(count));

		finalHttp.get(url, params, new AjaxCallBack<Object>() {
			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Log.d(TAG, "requestGetUserStatues fail :" + errorNo);
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_FAILER;
				msg.obj = errorNo;
				msg.sendToTarget();
			}

			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				Log.d(TAG, "requestGetUserStatues success :" + t.toString());
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_SUCCESS;
				JsonHelper helper = new JsonHelper();
				msg.obj = helper.getStatus(t.toString());
				msg.sendToTarget();
			}
		});

	}

	/**
	 * 获取登陆用户的粉丝列表
	 * 
	 * @param mHandler
	 * @param page
	 *            页码
	 * @param count
	 *            每页显示的数量
	 */
	public void requestGetFollowers(final Handler mHandler, int cursor,
			int count) {
		String url = "https://api.weibo.com/2/friendships/followers.json";
		params = new AjaxParams();
		params.put("access_token", access_token);
		params.put("uid", uid);
		params.put("cursor", String.valueOf(cursor));
		params.put("count", String.valueOf(count));

		finalHttp.get(url, params, new AjaxCallBack<Object>() {
			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Log.d(TAG, "requestGetFollowers fail :" + errorNo);
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_FAILER;
				msg.obj = errorNo;
				msg.sendToTarget();
			}

			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				Log.d(TAG, "requestGetFollowers success :" + t.toString());
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_SUCCESS;
				JsonHelper helper = new JsonHelper();
				msg.obj = helper.getUsers(t.toString());
				msg.sendToTarget();
			}
		});

	}

	/**
	 * 获取用户关注列表
	 * 
	 * @param mHandler
	 * @param page
	 *            页码
	 * @param count
	 *            每页显示的数量
	 */
	public void requestGetFriends(final Handler mHandler, int cursor, int count) {
		String url = "https://api.weibo.com/2/friendships/friends.json";
		params = new AjaxParams();
		params.put("access_token", access_token);
		params.put("uid", uid);
		params.put("cursor", String.valueOf(cursor));
		params.put("count", String.valueOf(count));

		finalHttp.get(url, params, new AjaxCallBack<Object>() {
			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Log.d(TAG, "requestGetFriends fail :" + errorNo);
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_FAILER;
				msg.obj = errorNo;
				msg.sendToTarget();

			}

			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				Log.d(TAG, "requestGetFriends success :" + t.toString());
				Message msg = mHandler.obtainMessage();
				msg.what = REQUEST_SUCCESS;
				JsonHelper helper = new JsonHelper();
				msg.obj = helper.getUsers(t.toString());
				msg.sendToTarget();
			}
		});
	}

	public void requestSetLikeStatus(final Handler mHandler, int status_id) {
		String url = "https://api.weibo.cn/2/like/set_like.json";
	}

	public void requestCancelLikeStatus(final Handler mHandler, int status_id) {
		String url = "https://api.weibo.cn/2/like/cancel_like.json";
	}
}
